package top.lv666.graduation_design_serve.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lv666.graduation_design_serve.pojo.Message;
import top.lv666.graduation_design_serve.service.MessageService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/message")
public class MessageController {
    // log4j
    public Logger logger = Logger.getLogger(UserController.class);
    //返回Map
    private final Map<String, Object> returnMap = new HashMap<>();

    @Autowired
    private MessageService messageService;

    /**
     * @apiNote 发布谋篇博客的评论
     */
    @PostMapping("/addBlogMessage")
    public Map<String, Object> CreateBlogMessage(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            map.put("request", request);
            messageService.CreateBlogMessage(map);
            returnMap.put("msg", "添加成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "添加失败！");
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 获取评论
     */
    @PostMapping("/getBlogMessage")
    public Map<String, Object> SelectBlogMessage(
            @RequestBody Map<String, Object> map
    ) {
        Map<String, Object> returnMap = new HashMap<>();
        try {
            List<Message> info = messageService.QueryBlogMessage(map);
            int count = messageService.getBlogMessageNumber((String) map.get("blogId"));
            returnMap.put("msg", "获取成功！");
            returnMap.put("flag", true);
            returnMap.put("count", count);
            returnMap.put("data", info);
        } catch (Exception | Error e) {
            returnMap.put("msg", "获取失败！");
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 二级评论
     */
    @PostMapping("/setCreateSecondaryMessage")
    public Map<String, Object> CreateSecondaryMessage(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        try {
            returnMap.clear();
            map.put("request", request);
            messageService.CreateSecondaryMessage(map);
            returnMap.put("msg", "评论成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "获取失败！");
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 二级评论获取
     */
    @PostMapping("/QuerySecondaryMessage")
    public Map<String, Object> QuerySecondaryMessage(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        try {
            returnMap.clear();
            map.put("request", request);
            returnMap.put("count", messageService.QuerySecondaryMessageCount(map));
            returnMap.put("data", messageService.QuerySecondaryMessageLimit(map));
            returnMap.put("msg", "获取成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "获取失败！");
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 二级评论点赞
     * */
//    @PostMapping("/")
}




